//
//  MuisicViewController.m
//  LO_MUSIC
//
//  Created by 胡林 on 16/2/24.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

#import "MuisicViewController.h"
#import "RequesURL.h"
#import "MusicPlayViewController.h"
#import "DownLoadImageOreration.h"
@interface MuisicViewController ()<UITableViewDataSource,UITableViewDelegate,DownloadImageOrerationDelegate>
@property(nonatomic ,strong)UITableView * rootTableView;
//数组模型
@property(nonatomic ,strong)NSMutableArray * modelArray;
//队列
@property (nonatomic ,strong)NSOperationQueue *ququ;

//存放图片的url
@property(nonatomic,strong)NSMutableDictionary *ImagrDic;
//存放
@property(nonatomic ,strong)NSMutableDictionary * operationDic;

@property(nonatomic ,assign,getter=IsScrolling) BOOL Scrolling;

@end



@implementation MuisicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音乐";
    self.view.backgroundColor = [UIColor redColor];
    self.rootTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.rootTableView.delegate = self;
    self.rootTableView.dataSource = self;
//    [self.rootTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"uitablecell"];
    [self.view addSubview:self.rootTableView];
    //请求数据
    __weak typeof(self) weakSelf = self;
    RequesURL * request = [[RequesURL alloc] init];
    [request requestMusiclistWithSuccess:^(id item) {
        weakSelf.modelArray = item;
        [weakSelf.rootTableView reloadData];
    } faile:^(NSError *err) {
        NSLog(@"%@",err);
    }];
    self.Scrolling = NO;
    
    // Do any additional setup after loading the view.
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

//每组多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}
////有多少组
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return 100;
//}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"uitablecell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"uitablecell"];
    }
    MusicMode * model= self.modelArray[indexPath.row];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.singer;
    
    //先去字典中查找
    if ([self.ImagrDic.allKeys containsObject:model.picUrl]) {
        //字典中存在图片
        //从字典中取出图片给imageView赋值
        cell.imageView.image = self.ImagrDic[model.picUrl];
    }else{
       //占位图
    cell.imageView.image = [UIImage imageNamed:@"屏幕快照 2016-02-25 下午2.10.13"];
        if (self.rootTableView.dragging || self.rootTableView.decelerating) {
            return cell;
        }


        //判断存放下载任务的字典中是不是正在下载
        if ([self.operationDic.allKeys containsObject:model.picUrl]) {
            
        }else{
            
        //如果没有下载
    //初始化自定义线程块
    DownLoadImageOreration * dowmImage = [[DownLoadImageOreration alloc] init];
    dowmImage.url = model.picUrl;
    dowmImage.delegate = self;
    dowmImage.indexPath = indexPath;

    //添加的队列
    [self.ququ addOperation:dowmImage];
    //添加到字典里
    [self.operationDic setValue:dowmImage forKey:dowmImage.url];
        }
    }
    return cell;
}
//滑动不加载
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (!decelerate) {
        [self lodaImage];
    }
}


//滑动结束
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self lodaImage];
}
-(void)lodaImage{
    //取出当前的可视的cell
    NSArray * visiblePaths = [_rootTableView indexPathsForVisibleRows];
    [self.rootTableView reloadRowsAtIndexPaths:visiblePaths withRowAnimation:(UITableViewRowAnimationNone)];
}
//

-(void)getImage:(UIImage *)image opreation:(DownLoadImageOreration *)opreation{
    UITableViewCell * cell = [self.rootTableView cellForRowAtIndexPath:opreation.indexPath];
    cell.imageView.image = image;
    //图片下载之后放在字典里面
    [self.ImagrDic setObject:image forKey:opreation.url];
    //从正在下载中移除
    [self.operationDic removeObjectForKey:opreation.url];
}
//
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MusicPlayViewController * music = [[MusicPlayViewController alloc] init];
    //传图片
//    MusicMode * model= self.modelArray[indexPath.row];
//    music.model = model;
//    music.songing = model.mp3Url;
    music.playArray = self.modelArray;
    music.num = indexPath.row;
    [self.navigationController pushViewController:music animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//
#pragma mark 懒加载
-(NSOperationQueue *)ququ{
    if (!_ququ) {
        _ququ = [[NSOperationQueue alloc]init];
        //最大并发数
        _ququ.maxConcurrentOperationCount = 3;
    }return _ququ;
}
-(NSMutableDictionary *)ImagrDic{
    if (!_ImagrDic) {
        _ImagrDic = [NSMutableDictionary dictionary];
        
    }return _ImagrDic;
}

-(NSMutableDictionary *)operationDic{
    if (!_operationDic) {
        _operationDic = [NSMutableDictionary dictionary];
    }return _operationDic;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
